package logic

import (
	"C/renrenzhuC_srv/renrenzhuC"
	"context"
	"fmt"
	"net/http"

	"C/api_C/internal/svc"
	"C/api_C/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderStatusLogic {
	return &OrderStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderStatusLogic) OrderStatus(w http.ResponseWriter, r *http.Request) (resp *types.OrderStatusResponse, err error) {
	// todo: add your logic here and delete this line
	fmt.Println("触发支付宝回调")
	fmt.Println(r)

	OrderSn := r.FormValue("out_trade_no")
	Status := r.FormValue("trade_status")

	if Status == "TRADE_SUCCESS" {
		_, err = l.svcCtx.RenrenzhuC_Srv.OrderStatus(l.ctx, &renrenzhuC.OrderStatusRequest{
			OrderSn: OrderSn,
			Status:  "已支付",
		})
		if err != nil {
			fmt.Println("订单回调失败")
		}
	}
	w.Write([]byte("success"))
	return
}
